<?php

$CLFetchers->fetchLstDataHeader($Db,
                                $jobId,
                                $SourceDb,
                                $nHeader,
                                $lstHeader);
if($isApprove){

  /**
   * @todo Mutate by production, rakitan or normal
   *
   * 1. check the balance
   *    a. check if rakitan
   *       i. check if rakitan (recursive)
   *      ii. multiply the child by its parent qty
   *     iii. sum per model id and check balance
   *          on error halt.
   *
   *    b. check if production or normal are treated here
   *       i. check balance
   *          on error halt.
   *
   * 2. add and sub the from-to-warehouse balance
   *
   * 3. process the SN status modification
   */

  /**
   * mutate only if sufficient balance
   */
  $CFetchers->fetchWhsBalance($Db,
                              $lstHeader["MODEL_ID"][0],
                              $lstHeader["UNIT"][0],
                              "WHS_".$lstHeader["FROM_WHS"][0],
                              $nWhsBal,
                              $fromQty);

  if(($fromQty - $lstHeader["QTY"][0]) < 0){

    $Misc->usrDieJs("Error: insufficient balance");
  }else{

    /* Do nothing */
  }

  /**
   * get how many model id
   * & check balance
   */

  /**
   * foreach model id check if composite
   */

  /**
   * foreach model id composite check if composite
   * & check balance
   */

  /**
   * if we have everything go
   * then process the add/sub
   * to whs balance
   * and update the SN
   */

  /**
   * Subtract from whs
   */
  $CHandlers->insToWhsBalance($Db,
                              $lstHeader["MODEL_ID"][0],
                              $lstHeader["UNIT"][0],
                              ($lstHeader["QTY"][0] * -1),
                              "WHS_".$lstHeader["FROM_WHS"][0]);

  /**
   * add to whs
   */
  $CHandlers->insToWhsBalance($Db,
                              $lstHeader["MODEL_ID"][0],
                              $lstHeader["UNIT"][0],
                              $lstHeader["QTY"][0],
                              "WHS_".$lstHeader["TO_WHS"][0]);

  $CFetchers->fetchLstDataDetail($Db,
                                 $jobId,
                                 $SourceDb->fetchValue("srcDbDtl"),
                                 $nDetail,
                                 $lstDetail);

  for($iRow = 0; $iRow < $nDetail ; $iRow++){

    $CHandlers->updSnWhsBySN($Db,
                             $lstDetail["SN"][$iRow],
                             $lstHeader["TO_WHS"][0],
                             $SourceDb);
  }
}else{

  /**
   * mutate only if sufficient balance
   */
  $CFetchers->fetchWhsBalance($Db,
                              $lstHeader["MODEL_ID"][0],
                              $lstHeader["UNIT"][0],
                              "WHS_".$lstHeader["TO_WHS"][0],
                              $nWhsBal,
                              $fromQty);

  if(($fromQty - $lstHeader["QTY"][0]) < 0){

    $Misc->usrDieJs("Error: insufficient balance");
  }else{

    /* Do nothing */
  }

  /**
   * Subtract to whs
   */
  $CHandlers->insToWhsBalance($Db,
                              $lstHeader["MODEL_ID"][0],
                              $lstHeader["UNIT"][0],
                              ($lstHeader["QTY"][0]* -1),
                              "WHS_".$lstHeader["TO_WHS"][0]);

  /**
   * add from whs
   */
  $CHandlers->insToWhsBalance($Db,
                              $lstHeader["MODEL_ID"][0],
                              $lstHeader["UNIT"][0],
                              $lstHeader["QTY"][0],
                              "WHS_".$lstHeader["FROM_WHS"][0]);

  $CFetchers->fetchLstDataDetail($Db,
                                 $jobId,
                                 $SourceDb->fetchValue("srcDbDtl"),
                                 $nDetail,
                                 $lstDetail);

  for($iRow = 0; $iRow < $nDetail ; $iRow++){

    $CHandlers->updSnWhsBySN($Db,
                             $lstDetail["SN"][$iRow],
                             $lstHeader["TO_WHS"][0],
                             $SourceDb);
  }
}

?>